package com.wangwei.config;

import com.wangwei.interceptor.SysInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web项目配置类
 *
 * @author wangwei_小锋
 * @site www.wangwei.com
 * @company 南通小锋网络科技有限公司
 * @create 2022-02-24 11:45
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS").maxAge(3600);
    }

    // 虚拟路径映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/swiper/**").addResourceLocations("file:/E:/Java/Code/dressSystem/images/swiperImgs/");
        registry.addResourceHandler("/image/bigType/**").addResourceLocations("file:/E:/Java/Code/dressSystem/images/bigTypeImgs/");
        registry.addResourceHandler("/image/product/**").addResourceLocations("file:/E:/Java/Code/dressSystem/images/productImgs/");
        registry.addResourceHandler("/image/productSwiperImgs/**").addResourceLocations("file:/E:/Java/Code/dressSystem/images/productSwiperImgs/");
        registry.addResourceHandler("/image/productIntroImgs/**").addResourceLocations("file:/E:/Java/Code/dressSystem/images/productIntroImgs/");
        registry.addResourceHandler("/image/productParaImgs/**").addResourceLocations("file:/E:/Java/Code/dressSystem/images/productParaImgs/");
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 排除指定的路径
        String[] patterns = new String[]{"/adminLogin", "/product/**", "/bigType/**", "/user/wxlogin", "/weixinpay/**", "/doc.html", "/swagger-ui.html", "/v2/api-docs", "/swagger-resources", "/webjars/**"};

        registry.addInterceptor(new SysInterceptor())
                .addPathPatterns("/**")          // 拦截所有路径
                .excludePathPatterns(patterns); // 排除指定的路径
    }


    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            System.out.println("测试=" + i);
        }
    }
}
